package com.base.cn.platform.os.service.user.exprule;


import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.dao.user.exprule.CusUserExpRuleDao;
import com.base.cn.platform.os.entity.user.exprule.CusUserExpRule;
import com.base.cn.platform.os.entity.user.exprule.CusUserExpRuleColumns;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 经验规则Biz
 * Created by ll on 2018/4/18.
 */
@Service
public class CusUserExpRuleBiz extends BaseBiz<CusUserExpRule,CusUserExpRuleDao> {

    /**
     * 分页搜索
     * @param cusUserExprule
     * @param page
     * @return
     */
    public PageInfo<CusUserExpRule> queryCusUserExpRulePage(CusUserExpRuleColumns cusUserExprule, Pagination page){
        String whereSql =whereSql(cusUserExprule);
        return this.findPage(whereSql,page,new ArrayList<>());
    }

    /**
     * 经验不翻页
     * @return
     */
    public List<CusUserExpRule> queryCusUserExpRuleList(CusUserExpRuleColumns cusUserExprule, List<String> containColumns){
        String where = whereSql(cusUserExprule);
        return this.find(where,null,containColumns);
    }

    /**
     * 经验Map<BigDecimal,CusUserExprule>
     * @return
     */
    public Map<BigDecimal,CusUserExpRule> queryCusUserExpRuleMap(CusUserExpRuleColumns cusUserExprule, Integer count, List<String> containColumns){
        String where = whereSql(cusUserExprule);
        List<CusUserExpRule> cusUserExprules =this.find(where,count,containColumns);
        Map<BigDecimal,CusUserExpRule> userExpruleMap = new HashMap<>();
        if (ObjectUtils.isNotEmpty(cusUserExprules)){
            userExpruleMap = cusUserExprules.stream().collect(Collectors.toMap(CusUserExpRule::getId, e->e));
        }
        return userExpruleMap;
    }

    /**
     * sql 语句
     * @param cusUserExprule
     * @return
     */
    public String  whereSql(CusUserExpRuleColumns cusUserExprule){
        String where =" status!=3 ";
        if(ObjectUtils.isNotEmpty(cusUserExprule)){
            if (ObjectUtils.isNotEmpty(cusUserExprule.getType())){
                if (DataUtil.idIsNotNull(BigDecimal.valueOf(cusUserExprule.getType()))){
                    where+= " and type ="+cusUserExprule.getType();
                }
            }
            if (ObjectUtils.isNotEmpty(cusUserExprule.getType())){
                if(DataUtil.idIsNotNull(BigDecimal.valueOf(cusUserExprule.getActionType()))){
                    where+= " and actionType ="+cusUserExprule.getActionType();
                }
            }
            if (StringUtils.isNotEmpty(cusUserExprule.getActionKeys())){
                where+= " and actionType in ("+cusUserExprule.getActionType()+")";
            }
            if (ObjectUtils.isNotEmpty(cusUserExprule.getType())){
                if(DataUtil.idIsNotNull(BigDecimal.valueOf(cusUserExprule.getStatus()))){
                    where+= " and status ="+cusUserExprule.getStatus();
                }
            }
            if (ObjectUtils.isNotEmpty(cusUserExprule.getIds())){
                where+= " and id in ("+cusUserExprule.getIds()+")";
            }
        }
        return where;
    }

    /**
     * 添加保存方法
     * @param userExprule
     * @return
     */
    public CusUserExpRule saveCusUserExpRule(CusUserExpRule userExprule) {
        if (DataUtil.idIsNotNull(userExprule.getId())) {
            CusUserExpRule _userExprule = this.findById(userExprule.getId());
            _userExprule.setScore(userExprule.getScore());
            this.updateById(_userExprule, null);
            return _userExprule;
        } else {
            userExprule.setStatus(1);
            this.save(userExprule);
            return userExprule;
        }
    }

    /**
     * 通过Id  停用经验规则
     * @param userExpruleId
     * @return
     */
    public CusUserExpRule updateUserExpRuleStatus(BigDecimal userExpruleId, Integer status){
        CusUserExpRule userExprule = this.findById(userExpruleId);
        userExprule.setStatus(status);
        this.updateById(userExprule,null);
        return userExprule;
    }
}
